package com.example.game_app.util;

import android.util.Log;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.sql.Connection;
import java.util.Map;
import java.util.Objects;

// 网络请求工具
public class Request {
    // 无线局域网适配器 WLAN IPV4
    public final static String HTTP_PREFIX = "http://192.168.115.106";
    public final static String PORT = ":8080";

    /**
     * 通过HTTP GET方法从指定的URL获取资源。
     *
     * @param url1 要访问的URL字符串。
     * @return 如果请求成功，则返回获取的资源字符串；如果请求失败或发生异常，则返回"-1"。
     */
    public String get(String url1) {
        try {
            // 创建URL对象，指定请求的地址
            URL url = new URL(HTTP_PREFIX + PORT + url1);

            // 打开与URL的连接
            HttpURLConnection Connection = (HttpURLConnection) url.openConnection();
            // 设置请求方法为GET
            Connection.setRequestMethod("GET");
            Connection.setConnectTimeout(3000);
            Connection.setReadTimeout(3000);
            // 获取服务器响应码
            int responseCode = Connection.getResponseCode();
            // 如果响应码为HTTP_OK（200），则请求成功
            if (responseCode == HttpURLConnection.HTTP_OK) {
                // 获取服务器响应的输入流
                InputStream inputStream = Connection.getInputStream();
                // 使用ByteArrayOutputStream来存储流数据
                ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
                // 定义一个缓冲区，用于读取流数据
                byte[] bytes = new byte[1024];
                int length = 0;
                while ((length = inputStream.read(bytes)) != -1) {
                    arrayOutputStream.write(bytes, 0, length);
                    arrayOutputStream.flush();
                }
                // 将ByteArrayOutputStream转换为字符串并返回
                return arrayOutputStream.toString();
            } else {
                // 如果响应码不为HTTP_OK，则请求失败，返回"-1"
                return "-1";
            }
        } catch (Exception e) {
            return "-1";
        }
    }


    public static String post(String url1, String data) {
        try {
            URL url = new URL(HTTP_PREFIX + PORT + url1);
            HttpURLConnection Connection = (HttpURLConnection) url.openConnection();// 创建连接
            Connection.setRequestMethod("POST");
            Connection.setConnectTimeout(3000);
            Connection.setReadTimeout(3000);
            Connection.setDoInput(true);
            Connection.setDoOutput(true);
            Connection.setUseCaches(false);
            Connection.connect();
            // 默认表单请求形式
            DataOutputStream dos = new DataOutputStream(Connection.getOutputStream());
            // 参数转字节数组写入
            dos.write(data.getBytes());
            dos.flush();
            dos.close();// 写完记得关闭
            int responseCode = Connection.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {// 判断请求是否成功
                InputStream inputStream = Connection.getInputStream();
                ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
                byte[] bytes = new byte[1024];
                int length = 0;
                while ((length = inputStream.read(bytes)) != -1) {
                    arrayOutputStream.write(bytes, 0, length);
                    arrayOutputStream.flush();
                }// 读取响应体的内容
                return arrayOutputStream.toString();// 返回请求到的内容，字符串形式
            } else {
                return "-1";
            }
        } catch (Exception e) {
            return "-1";
        }
    }

}